//Documentation complete
/**
 * @author Jason
 * @version 2009AR06
 */

package control;

import broker.*;
import problemDomain.*;
import problemDomain.ErrorBean.ErrorCode;

import java.util.Date;

/**
 * Control object for sales per time period report.
 */
public class SalesTimePeriodReportControl extends Control {
	
	/* Attributes */
	
	private SalesPerTimePeriodReportBroker stpb;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>SalesTimePeriodReportControl</code>.
	 * @throws ErrorBean if the database is unavailable
	 */
	public SalesTimePeriodReportControl () throws ErrorBean {
		stpb = SalesPerTimePeriodReportBroker.getBroker();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	/**
	 * Generates the report.
	 * @param from the date to lower bound the report
	 * @param to the date to upper bound the report
	 * @param timePeriod the length of time to divide the report into (ex. day, month, etc.)
	 * @return the report
	 * @throws ErrorBean if the input data are not correct
	 */
	public SalesPerTimePeriodReportBean report (String from, String to, String timePeriod) throws ErrorBean {
		SalesPerTimePeriodReportBean report = new SalesPerTimePeriodReportBean();
		
		Date dFrom = checkDate(from);
		Date dTo = checkDate(to);
		
		report.setReportList(stpb.getSalesPerTimePeriodReport(dFrom, dTo, timePeriod));
		report.setTotal(stpb.getTotalReport(dFrom, dTo));
		report.setTimePeriod(timePeriod);
		
		report.isValid();
		
		return report;
	}
	
	/* END Methods */
	
	
	/* Data Checks */
	
	private Date checkDate (String date) throws ErrorBean {
		return checkDate(date, ErrorCode.REPORT_INVALID_DATE, this);}
	
	/* END Data Checks */
	
}
